package devcken.validation;

import java.util.Locale;

import javax.validation.MessageInterpolator;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.MessageSource;
import org.springframework.context.MessageSourceAware;

/**
 * 제약 사항 위반에 관한 메시지를 {@link org.springframework.context.MessageSource}로부터 가져와 교체해주는 역할을 한다.
 * @author Devcken
 */
public class ValidatorResourceMessageInterpolator implements MessageInterpolator, MessageSourceAware, InitializingBean
{
	private MessageSource _messageSource;

	@Override
	public String interpolate(String messageTemplate, Context context)
	{
		return this._messageSource.getMessage(messageTemplate, new Object[] { }, Locale.getDefault());
	}

	@Override
	public String interpolate(String messageTemplate, Context context, Locale locale)
	{
		return this._messageSource.getMessage(messageTemplate, new Object[] { }, locale);
	}
	
	@Override
	public void afterPropertiesSet() throws Exception
	{
		if (this._messageSource == null)
		{
			throw new IllegalStateException(String.format("MessageSource property wasn't injected, so couldn't initialize '%s'", this.getClass().getName()));
		}
	}
	
	@Override
	public void setMessageSource(MessageSource messageSource)
	{
		this._messageSource = messageSource;
	}
}